package com.jeeplus.modules.sys.web;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sys.utils.HtmlUtil;


@Controller
@RequestMapping(value = "${adminPath}/sys/officeHome")
public class OfficeHomeController {
		
	@Autowired
	private OfficeService officeService;
	
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute("office")
	public Office get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return officeService.get(id);
		}else{
			return new Office();
		}
	}
	
	@RequiresPermissions("sys:office:index")
	@RequestMapping(value = {""})
	public String index(Office office, Model model) {
//        model.addAttribute("list", officeService.findAll());
		   List<Office> findList = officeService.findListLike(null);
	        for (Office office2 : findList) {
	        	String html = org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(office2.getContent());
	        	if(org.apache.commons.lang3.StringUtils.isNoneBlank(html)){
	        		 office2.setContent(HtmlUtil.getTextFromHtml(html));
	        	}
	        	 //List<User> officeUser= systemService.findUserByOfficeId(office2.getId());
	        	 //office2.setUser(officeUser);
			}
	        model.addAttribute("officeList", findList);
		return "modules/sys/officeHome";
	}

	@RequiresPermissions("sys:office:index")
	@RequestMapping(value = {"list"})
	@ResponseBody
	public List<Office> getOfficeList(String keywords, Model model) {
        List<Office> findList = officeService.findListLike(keywords);
        for (Office office2 : findList) {
        	 office2.setContent(org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(office2.getContent()));
        	 //List<User> officeUser= systemService.findUserByOfficeId(office2.getId());
        	 //office2.setUser(officeUser);
		}
		return findList;
	}
	@RequiresPermissions("sys:office:index")
	@RequestMapping(value = {"officeDetail"})
	public String officeDetail(String id,Office office, Model model) {
		office.setContent(org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(office.getContent()));
		model.addAttribute("office", office);
		
		return "modules/sys/officeDetail";
	}
}
